package com.foreknow.demo0928;

/**
 * 主人类
 * 实现主人带着宠物看病
 */
public class Master {

    public void cure(Pet p) {
        if (p.getHealth() < 50) {
            p.toHospital();
        }
    }

    public void play(Pet p) {
        //如果想要调用Dog Penguin 中新扩充的方法，
        //需要将Pet类型强制类型转化为子类型(Dog Penguin)
        if (p instanceof Dog) {
            Dog dog = (Dog) p;
            dog.catchfly();
        }else if (p instanceof Penguin) {
            Penguin peg = (Penguin) p;
            peg.swim();
        }
    }

    public static void main(String[] args) {
        Master m = new Master();
        Pet p = new Dog();
        p.setName("三金");
        p.setHealth(30);
        p.setLove(0);
        m.cure(p);
        m.play(p);
        System.out.println("---------------");
        p = new Penguin();
        p.setName("QQ");
        p.setHealth(20);
        p.setLove(10);
        m.cure(p);
        m.play(p);
    }
}
